package cn.fun.spring4.chapter2.service;

import cn.fun.spring4.chapter2.dao.LoginLogDao;
import cn.fun.spring4.chapter2.dao.UserDao;
import cn.fun.spring4.chapter2.domain.LoginLog;
import cn.fun.spring4.chapter2.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;

/**
 * @author zhangxf
 * @title UserService
 * @date 2018/2/27
 * @description
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private LoginLogDao loginLogDao;

    public boolean hasMatchUser(String userName, String password) {
        int matchCount =userDao.getMatchCount(userName, password);
        return matchCount > 0;
    }

    public User findUserByUserName(String userName) {
        return userDao.findUserByUserName(userName);
    }

    @Transactional
    public void loginSuccess(User user) {
        user.setCredits( 5 + user.getCredits());
        LoginLog loginLog = new LoginLog();
        loginLog.setUserId(user.getUserId());
        loginLog.setIp(user.getLastIp());
        loginLog.setLoginDate(user.getLastVisit());
        userDao.updateLoginInfo(user);
        // System.out.println(1/0);
        loginLogDao.insertLoginLog(loginLog);
    }

}
